/*
Copyright (c) 2013, Maxim Savin.
*/

(function()
{
	CKEDITOR.plugins.add( 'keywords',
	{
		requires : [ 'richcombo', 'styles' ],

		init : function( editor )
		{
			var config = editor.config,
				lang = editor.lang.stylesCombo,
				keywords = {
                                    'parents':'Список'
                                },
				combo;

			editor.ui.addRichCombo( 'Keywords',
				{
					label : 'Элементы сайта',
					title : 'Элементы',
					className : 'cke_styles',

					panel :
					{
						css : editor.skin.editor.css.concat( config.contentsCss ),
						multiSelect : false,
						attributes : { 'aria-label' : lang.panelTitle }
					},

					init : function()
					{
						combo = this;

                                                for ( i in keywords )
                                                {
                                                    combo.add(
                                                            i,
                                                            keywords[i],
                                                            keywords[i] );
                                                }

                                                combo.commit();
					},

					onClick : function( value )
					{
						editor.focus();
						editor.fire( 'saveSnapshot' );

						var name = keywords[ value ],
							selection = editor.getSelection().getStartElement(),
							elementPath = new CKEDITOR.dom.elementPath( selection );
                                                var span = document.createElement('span');
                                                span.innerHTML('['+value.toUpperCase()+']')
                                                editor.insertElement( span );

						editor.fire( 'saveSnapshot' );
					},

					onRender : function()
					{
						editor.on( 'selectionChange', function( ev )
							{
								var currentValue = this.getValue(),
									elementPath = ev.data.path,
									elements = elementPath.elements;

								// For each element into the elements path.
								for ( var i = 0, count = elements.length, element ; i < count ; i++ )
								{
									element = elements[i];

									// Check if the element is removable by any of
									// the styles.
									for ( var value in keywords )
									{
										if ( keywords[ value ].checkElementRemovable( element, true ) )
										{
											if ( value != currentValue )
												this.setValue( value );
											return;
										}
									}
								}

								// If no styles match, just empty it.
								this.setValue( '' );
							},
							this);
					},

					onOpen : function()
					{
						if ( CKEDITOR.env.ie || CKEDITOR.env.webkit )
							editor.focus();

						var selection = editor.getSelection(),
							element = selection.getSelectedElement(),
							elementPath = new CKEDITOR.dom.elementPath( element || selection.getStartElement() ),
							counter = [ 0, 0, 0, 0 ];

						this.showAll();
						this.unmarkAll();
						for ( var name in keywords )
						{
							if ( name.toUpperCase() === element.innerHTML.replace(/^\[|\]$/g,'') )
								this.mark( name );
                                                            
							counter[ type ]++;
						}
					},

					// Force a reload of the data
					reset: function()
					{
						if ( combo )
						{
							delete combo._.panel;
							delete combo._.list;
							combo._.committed = 0;
							combo._.items = {};
							combo._.state = CKEDITOR.TRISTATE_OFF;
						}
					}
				});

			editor.on( 'instanceReady', function() {} );
		}
	});

	function sortStyles( styleA, styleB )
	{
		var typeA = styleA.type,
			typeB = styleB.type;

		return typeA == typeB ? 0 :
			typeA == CKEDITOR.STYLE_OBJECT ? -1 :
			typeB == CKEDITOR.STYLE_OBJECT ? 1 :
			typeB == CKEDITOR.STYLE_BLOCK ? 1 :
			-1;
	}
})();
